//
//  PushDeposeCenter.m
//  Keping
//
//  Created by Joanlove on 2018/8/2.
//  Copyright © 2018年 柯平. All rights reserved.
//

#import "PushDeposeCenter.h"
#import "HJInOrOutRecordsController.h"
#import "KDoorAccessoryViewController.h"
#import "KNewsGeneralDetailController.h"
#import "KNewsPersonalDetailController.h"
#import "KWallDetailViewController.h"
#import "KGeneralCommentsController.h"
#import "HJAppointmentsController.h"

@implementation PushDeposeCenter

SYNTHESIZE_SINGLETON_FOR_CLASS(PushDeposeCenter)

-(void)deposeWithParama:(NSDictionary *)remoteInfo{
    NSLog(@"get received:%@",remoteInfo);
    [self performSelector:@selector(jumpDepose:) withObject:remoteInfo afterDelay:1];
}

-(void)jumpDepose:(NSDictionary *)remoteInfo
{
    /*
    1=门禁进出记录；
     2=公告信息；
     3=私信；
     4=系统信息(system=社区消息)；
     5=系统信息（sos=紧急消息）；
     6=app版本更新信息；
     7=评论，评论回复
     8=公告评论，评论回复
     9=紧急呼叫图片，点击不跳转
     10=紧急呼叫测试，点击不跳转
     12=拜访邀请通过
     13=好友发送拜访，
     14=好友动态点赞
     */
    NSInteger type = [remoteInfo[@"messageType"] integerValue];
    UINavigationController *curNav = [self getRootViewController];
    if (type == 1) {
        if (![KAppConfig sharedConfig].veritify) {
            UIViewController *vc = [self currentViewController];
            if (![vc isKindOfClass:[KDoorAccessoryViewController class]]) {
                KDoorAccessoryViewController *door = [[KDoorAccessoryViewController alloc] init];
                [curNav pushViewController:door animated:YES];
            }
            return;
        }
        UIViewController *vc = [self currentViewController];
        if ([vc isKindOfClass:[HJInOrOutRecordsController class]]) {
            HJInOrOutRecordsController *record = (HJInOrOutRecordsController *)vc;
            [record setUI];
        } else {
            HJInOrOutRecordsController *record = [[HJInOrOutRecordsController alloc] init];
            [curNav pushViewController:record animated:YES];
        }
    } else if (type == 2){
        UIViewController *vc = [self currentViewController];
        if ([vc isKindOfClass:[KNewsGeneralDetailController class]]) {
            KNewsGeneralDetailController *detail = (KNewsGeneralDetailController *)vc;
            if ([detail.bbsTopicViewsID isEqualToString:remoteInfo[@"id"]]) {
                [detail getData];
            } else {
                KNewsGeneralDetailController *detail = [[KNewsGeneralDetailController alloc] init];
                detail.bbsTopicViewsID = remoteInfo[@"id"];
                [curNav pushViewController:detail animated:YES];
            }
        } else {
            KNewsGeneralDetailController *detail = [[KNewsGeneralDetailController alloc] init];
            detail.bbsTopicViewsID = remoteInfo[@"id"];
            [curNav pushViewController:detail animated:YES];
        }
        
    } else if (type == 3) {
        UIViewController *vc = [self currentViewController];
        if ([vc isKindOfClass:[KNewsPersonalDetailController class]]) {
            KNewsPersonalDetailController *personal = (KNewsPersonalDetailController *)vc;
            if ([personal.personalID isEqualToString:remoteInfo[@"id"]]) {
                [personal setUI];
            } else {
                KNewsPersonalDetailController *personal = [[KNewsPersonalDetailController alloc] init];
                personal.personalID = remoteInfo[@"id"];
                [curNav pushViewController:personal animated:YES];
            }
        } else {
            KNewsPersonalDetailController *detail = [[KNewsPersonalDetailController alloc] init];
            detail.personalID = remoteInfo[@"id"];
            [curNav pushViewController:detail animated:YES];
        }
    } else if (type == 5) {
        
    } else if (type == 6) {
        NSString * url = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=%@",@"1400634234"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    } else if (type == 7) {
        UIViewController *vc = [self currentViewController];
        if ([vc isKindOfClass:[KGeneralCommentsController class]]) {
            KGeneralCommentsController *detail = (KGeneralCommentsController *)vc;
            if ([detail.bbsPostID isEqualToString:remoteInfo[@"id"]]) {
                [detail getData];
            } else {
                KGeneralCommentsController *detail = [[KGeneralCommentsController alloc] init];
                detail.bbsPostID = remoteInfo[@"id"];
                [curNav pushViewController:detail animated:YES];
            }
        } else {
            
            KGeneralCommentsController *Comments = [KGeneralCommentsController new];
            Comments.bbsPostID = remoteInfo[@"id"];;
            [curNav pushViewController:Comments animated:YES];
        }
    } else if (type == 8) {
        UIViewController *vc = [self currentViewController];
        if ([vc isKindOfClass:[KGeneralCommentsController class]]) {
            KGeneralCommentsController *comments = (KGeneralCommentsController *)vc;
            if ([comments.bbsPostID isEqualToString:remoteInfo[@"id"]]) {
                [comments getData];
            } else {
                KGeneralCommentsController *comments = [[KGeneralCommentsController alloc] init];
                comments.bbsPostID = remoteInfo[@"id"];
                [curNav pushViewController:comments animated:YES];
            }
        } else {
            KGeneralCommentsController *comments = [[KGeneralCommentsController alloc] init];
            comments.bbsPostID = remoteInfo[@"id"];
            [curNav pushViewController:comments animated:YES];
        }
    } else if (type == 9) {
        
    } else if (type == 10) {
        
    } else if (type == 11) {
        
    } else if (type == 12) {
        UIViewController *vc = [self currentViewController];
        if ([vc isKindOfClass:[HJAppointmentsController class]]) {
            HJAppointmentsController *appoint = (HJAppointmentsController *)vc;
            if (appoint.selectIndex != 0) {
                [appoint clickHeadSelectIndex:0];
            }
            return;
        } else {
            HJAppointmentsController *appoint = [[HJAppointmentsController alloc] init];
            [curNav pushViewController:appoint animated:YES];
        }
    } else if (type == 13) {
        UIViewController *vc = [self currentViewController];
        if ([vc isKindOfClass:[HJAppointmentsController class]]) {
            HJAppointmentsController *appoint = (HJAppointmentsController *)vc;
            if (appoint.selectIndex != 1) {
                [appoint clickHeadSelectIndex:1];
            }
            return;
        } else {
            HJAppointmentsController *appoint = [[HJAppointmentsController alloc] initWithIndex:1];
            [curNav pushViewController:appoint animated:YES];
        }
    } else if (type == 14) {
        UIViewController *vc = [self currentViewController];
        if ([vc isKindOfClass:[KWallDetailViewController class]]) {
            KWallDetailViewController *detail = (KWallDetailViewController *)vc;
            if ([detail.wallID isEqualToString:remoteInfo[@"id"]]) {
                [detail getData];
            } else {
                KWallDetailViewController *detail = [[KWallDetailViewController alloc] init];
                detail.wallID = remoteInfo[@"id"];
                [curNav pushViewController:detail animated:YES];
            }
        } else {
            KWallDetailViewController *detail = [[KWallDetailViewController alloc] init];
            detail.wallID = remoteInfo[@"id"];
            [curNav pushViewController:detail animated:YES];
        }
    }
}

- (UINavigationController *)getRootViewController{
    AppDelegate *del =(AppDelegate*)[[UIApplication sharedApplication]delegate];
    UITabBarController *tabControl= (UITabBarController*)[del.window rootViewController];
    UINavigationController *curNav = (UINavigationController *)tabControl.viewControllers[tabControl.selectedIndex];
    return curNav;
}

//获取Window当前显示的ViewController
- (UIViewController*)currentViewController{
    //获得当前活动窗口的根视图
    UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (1)
    {
        //根据不同的页面切换方式，逐步取得最上层的viewController
        if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = ((UITabBarController*)vc).selectedViewController;
        }
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = ((UINavigationController*)vc).visibleViewController;
        }
        if (vc.presentedViewController) {
            vc = vc.presentedViewController;
        }else{
            break;
        }
    }
    return vc;
}

@end
